<link rel="stylesheet" href="/lib/fromSelects/formSelects-v4.css"/>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>站点信息</legend>
        </fieldset>
        <div class="layui-form layuimini-form" lay-filter="fromSite">
            <div class="layui-form-item">
                <input type="text" hidden name="siteId"/>
                <div class="layui-inline">
                    <label class="layui-form-label  ">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="siteName" disabled="disabled" value="" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label  ">域名</label>
                    <div class="layui-input-block">
                        <input type="text" name="siteDomain" disabled="disabled"
                               value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label  ">目录</label>
                    <div class="layui-input-block">
                        <input type="text" name="siteDescribe" disabled="disabled" value="" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label required">站点描述</label>
                <div class="layui-input-block">
                    <textarea name="siteDescribe" disabled="disabled" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label required">配置文件</label>
                <div class="layui-input-block">
                    <select   name="configIds" id="siteConfigOP" lay-verify="required"
                            lay-filter="configFilter">
                        <option value=""></option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label required">选择执行文件</label>
                <div class="layui-input-block">
                    <select name="executableFileId" id="executableOP" lay-verify="required"
                            lay-filter="executableFilter">
                        <option value=""></option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label required">shell脚本</label>
                <div class="layui-input-block">
                <textarea name="siteShell" placeholder="请输入内容" lay-reqtext="shell脚本不能为空" lay-verify="required"
                          class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="execute">构建</button>

                </div>
            </div>
        </div>


        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>日志</legend>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <!--                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>-->
            </div>
        </script>

        <table class="layui-hide" id="subCurrentTableId" lay-filter="subCurrentTableFilter"></table>

        <script type="text/html" id="subCurrentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="preview">预览</a>
            <!--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>-->
        </script>
    </div>
</div>

<script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>

<script src="/lib/fromSelects/formSelects-v4.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'common', 'miniPage', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            common = layui.common
            , miniPage = layui.miniPage
            , laydate = layui.laydate;
        $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();
        var siteId = parent.siteId;
        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        common.ajaxGet('/api/executable/file/list', function (_data) {
                if (_data.resCode === '0000') {
                    for (var i = 0; i < _data.resultList.length; i++) {
                        $("#executableOP").append("<option value=\"" + _data.resultList[i].id + "\">" +
                            _data.resultList[i].executableName
                            + '(' + _data.resultList[i].originalFileName + ')' + "</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }
            }
        )

        var whereData = {siteId: siteId}
        common.ajaxGet('/api/site/config/list?search=' + encodeURI(JSON.stringify(whereData)), function (_data) {
                if (_data.resCode === '0000') {
                    for (var i = 0; i < _data.resultList.length; i++) {
                        $("#siteConfigOP").append("<option value=\"" + _data.resultList[i].id + "\">" +
                            _data.resultList[i].fileName +
                            '(' + _data.resultList[i].originalFileName + ')' +
                            "</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }
            }
        )

        common.ajaxGet('/api/server/site/detail?id=' + siteId, function (_data) {
                if (_data.resCode === '0000') {
                    form.val("fromSite", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                        "siteId": _data.data.id // "name": "value"
                        , "siteName": _data.data.siteName
                        , "sitePort": _data.data.sitePort
                        , "siteStatus": _data.data.siteStatus
                        , "siteDomain": _data.data.siteDomain
                        , "siteDescribe": _data.data.siteDescribe
                        , "siteDirectory": _data.data.siteDirectory
                        , "siteShell": _data.data.siteShell
                    });
                }
            }
        )
        form.on('submit(execute)', function (data) {
            var loading = layer.load(
                1, //0,1,2  loading风格
                {
                    shade: true, // 是否有遮罩，true表示有遮罩
                    time: 5 * 1000 // 设定最长等待时间,设置时间之后，loading会在时间到之后自动关闭
                });
            var _data = form.val("fromSite");
            common.ajaxPost('/api/refactor/execute',
                JSON.stringify(_data)
                , function (_data) {
                    if (_data.resCode === '0000') {
                        layer.close(loading);
                        layer.msg(_data.resDesc, {icon: 6});
                        table.reload('subCurrentTableId');
                    } else {
                        layer.close(loading);
                        layer.msg(_data.resDesc, {icon: 7});
                    }
                }
            )
            return false;
        });


        var whereData = {siteId: siteId}
        table.render({
            elem: '#subCurrentTableId',
            url: '/api/release/log/page/list?search=' + encodeURI(JSON.stringify(whereData)),
            data: JSON.stringify(whereData),
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id', hide: true},
                    {field: 'executableFileVersion', width: 200, title: '文件版本'},
                    {field: 'releaseTime', width: 200, title: '重构时常(毫秒)'},
                    {field: 'releaseStatus', minWidth: 180, title: '状态'},
                    // {field: 'siteType', minWidth: 180, title: '站点类型'},
                    // {field: 'siteStatus', minWidth: 180, title: '站点状态'},
                    {fixed: 'right', title: '操作', minWidth: 100, toolbar: '#subCurrentTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        table.on('tool(subCurrentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'preview') {
                console.log(obj.data.path)
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: false,
                    shadeClose: false,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: obj.data.path,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }

        });
    });
</script>